package router

import (
	"app/app/controllers"
	"app/app/middlewares"
	"app/configs"
	"net/http"
	"os"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"
)

func InitRouter() {
	app := iris.New()
	app.AllowMethods(iris.MethodOptions)
	app.RegisterView(iris.HTML("./views", ".html"))
	app.HandleDir("/", "./views", iris.DirOptions{
		IndexName: "/index.html", // default "/index.html"
	})
	app.Use(middlewares.AuthFilter)
	mvc.Configure(app.Party("/api"), func(m *mvc.Application) {
		m.Party("/user").Handle(new(controllers.UserController))
		m.Party("/partner").Handle(new(controllers.PartnerController))
		m.Party("/member").Handle(new(controllers.MemberController))
	})
	mvc.Configure(app.Party("/"), func(m *mvc.Application) {
		m.Party("/login").Handle(new(controllers.LoginController))
		m.Party("/logout").Handle(new(controllers.LogOutController))
	})

	server := &http.Server{Addr: ":" + configs.Instance.Port}
	err := app.Run(iris.Server(server), iris.WithConfiguration(iris.Configuration{
		DisableStartupLog:                 false,
		DisableInterruptHandler:           false,
		DisablePathCorrection:             false,
		EnablePathEscape:                  false,
		FireMethodNotAllowed:              false,
		DisableBodyConsumptionOnUnmarshal: false,
		DisableAutoFireStatusCode:         false,
		EnableOptimizations:               true,
		TimeFormat:                        "2006-01-02 15:04:05",
		Charset:                           "UTF-8",
	}))
	if err != nil {
		os.Exit(-1)
	}
}
